package lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 需求：将一个字符串列表，全部转换成大写，并且用逗号加空格连接起来
 */
public class OldDemo {
    /**
     * JAVA 1.7以前的老做法
     */
    public static String oldMethod(List<String> list) {
        List<String> tempList = new ArrayList<>();
        for (String s : list) {
            tempList.add(s.toUpperCase());
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < tempList.size(); i++) {
            result.append(tempList.get(i));
            if (i != tempList.size() - 1) {
                result.append(", ");
            }
        }
        return result.toString();
    }

    /**
     * JAVA 1.8之后使用Stream
     */
    public static String newMethod(List<String> list) {
        return list.stream().map(String::toUpperCase)
                .collect(Collectors.joining(", "));
    }

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("usa", "china", "japan", "cup", "boy", "girl", "java");
        System.out.println(oldMethod(strings));
        System.out.println(newMethod(strings));
    }
}
